Imports System.Data
Imports DAOMain

Partial Class Accounting_CashAdvance
    Inherits WebPageCommon

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sec As New Secure

        With sec
            .ParentPage = Me
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)

            .SetPageAccess(Session("UserName"))
        End With

        sec = Nothing
    End Sub

    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call DoSearch()
    End Sub

    Private Sub DoSearch()
        Dim vo As New VW_USER_CASH_ADVANCE_MSTR_SEARCH_VO
        Dim cf As New CIMSfunc

        With vo
            If Trim(Startdate_txt.Text) <> "" Then .CASH_ADV_DT = cf.ParseSetDate(Startdate_txt.Text)
            If Trim(Enddate_txt.Text) <> "" Then .CASH_ADV_DT_END = cf.ParseSetDate(Enddate_txt.Text)
            .NAME = "%" & Client_txt.Text & "%"
        End With

        ViewState.Add("vo", vo)
        DataGrid.CurrentPageIndex = 0
        Call BindGrid()

        cf = Nothing
        vo = Nothing
    End Sub

    Private Sub BindGrid()
        If Not IsNothing(ViewState("vo")) Then
            Dim dao As New VW_USER_CASH_ADVANCE_MSTR_SEARCH_DAO

            Dim dt As DataTable = dao.Search(ViewState("vo"), , True)
            results_label.Text = dt.Rows.Count

            DataGrid.DataSource = dt
            DataGrid.DataBind()

            dao = Nothing
        End If
    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        Select Case e.CommandName

            Case "Edit"
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("mode", "edit")

                Response.Redirect("~/Accounting/CashAdvanceDetails.aspx")

            Case "View"
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("mode", "view")

                Response.Redirect("~/Accounting/CashAdvanceDetails.aspx")

            Case "Delete"
                Dim vo As New USER_CASH_ADVANCE_MSTR_VO
                Dim dao As New USER_CASH_ADVANCE_MSTR_DAO

                vo.CASH_ADV_SEQ_CD = e.Item.Cells(0).Text

                If dao.Delete(vo) Then
                    Call DoSearch()
                End If

                vo = Nothing
                dao = Nothing
        End Select
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        DataGrid.CurrentPageIndex = e.NewPageIndex

        Call BindGrid()
    End Sub

End Class
